package com.ls.lserp.modules.setting.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.modules.setting.entity.InventoryWarning;
import com.ls.lserp.modules.setting.dao.InventoryWarningDao;
import com.ls.lserp.modules.common.entity.UploadFile;
import com.ls.lserp.modules.sys.dao.UploadFileDao;
import com.ls.lserp.common.utils.CollectionsUtils;


/**
  * @ClassName: InventoryWarningService
  * @Description: 库存 保存 修改 删除
  * 
  * @author 
  * @date 2017年2月27日 上午9:55:34
  *
  */

@Service
@Transactional(readOnly = true)
public class InventoryWarningService extends CrudService<InventoryWarningDao, InventoryWarning>{
    
    @Autowired
    private InventoryWarningDao  inventoryWarningDao;
    
    @Autowired
	private UploadFileDao  uploadFileDao;
      
     /**
         * @Title: findList
         * @Description: 一览
         * @param @param page
         * @param @param companyUser
         * @param @return
         * @return Page<CompanyUser>
         * @throws
         */

        public Page<InventoryWarning> findList(Page<InventoryWarning> page, InventoryWarning inventoryWarning) {
            // 设置分页参数
        	inventoryWarning.setPage(page);
            // 执行分页查询
            page.setList(inventoryWarningDao.findList(inventoryWarning));
            
            return page;
        }
    
	/**
	  * @Title: getMaterial
	  * @Description: 获得我的物料信息
	  * @param @param id
	  * @param @return
	  * @return Material 
	  * @throws
	  */
	
/*	public Material getMaterial(Long id){
	       // 实力化实体类
		   Material material=new Material();
	       
		   // 执行查询语句，获取基本信息
		   material=materialDao.get(id);
	        
	        return material;
	    }*/



	
	/**
	  * @Title: saveInventory
	  * @Description: 保存库存
	  * @param @param material
	  * @return void 
	  * @throws
	  */
	
	@Transactional(readOnly = false)
	public void saveInventory(InventoryWarning inventoryWarning) {
		
		// 保存图片
		if(inventoryWarning.getUploadFile()!=null){
			
		    UploadFile uploadFile=inventoryWarning.getUploadFile();
        	
		    if(!CollectionsUtils.isEmpty(uploadFileDao.searchFileId(uploadFile))){
        		
        		// 返回查询结果id
		    	inventoryWarning.getMaterial().setFileId(uploadFile.getFileId());
        	}else{
        	    uploadFile.preInsert();
        		// 若为null 执行保存操作
        	    uploadFileDao.saveUploadFile(uploadFile);
        		
        	    inventoryWarning.getMaterial().setFileId(uploadFile.getFileId());
        		
        	}
		}
		
		// 判断新建 编辑
		// 若为null时执行插入操作（新建）
		if (inventoryWarning.getId()==null){
			inventoryWarning.preInsert();
			inventoryWarningDao.insert(inventoryWarning);
			
		// 编辑
		}else{
			// 更新数据
			inventoryWarning.preUpdate();
			inventoryWarningDao.update(inventoryWarning);
		}
		
		
	}

	/**
	  * @Title: delete
	  * @Description: 删除库存
	  * @param @param deleteInventory
	  * @return void 
	  * @throws
	  */
	
	@Transactional(readOnly = false)
	public void deleteInventory(InventoryWarning inventoryWarning) {
		
		// 逻辑删除
		inventoryWarningDao.delete(inventoryWarning);
	}
	
	
     /**
	  * @Title: getByName
	  * @Description: 查重
	  * @param @param materialNameCn
	  * @param @return
	  * @return Material 
	  * @throws
	  *//*
	
	public Material getByCode(String Code) {
		
		// 实例化实体类
		Material material = new Material();
		material.setMaterialCode(Code);
		
		// 执行查询返回查询结果
		return materialDao.getByCode(material);
	}*/
}
